<template>
	<view class="flex-col homepage">
		<view class="flex-row wrapper">
			<u-input class="input" v-model="VehicleCar.vehicleNumber"></u-input>
			<view class="u-line-1 text">车牌号</view>
		</view>
		<u-line class="line" margin="0rpx 0rpx 24rpx 0rpx"></u-line>
		<view class="flex-row wrapper-two">
			<u-input class="input" v-model="VehicleCar.vehicleBrand"></u-input>
			<view class="u-line-1 text-two">车辆品牌</view>
		</view>
		<u-line class="line" margin="0rpx 0rpx 38rpx 0rpx"></u-line>
		<view class="flex-row wrapper-three">
			<u-input class="input" v-model="VehicleCar.vehicleModel"></u-input>
			<view class="u-line-1 text-three">车辆型号</view>
		</view>
		<u-line class="line" margin="0rpx 0rpx 40rpx 0rpx"></u-line>
		<view class="flex-row wrapper-four">
			<view class="u-line-1 text-four">车身颜色</view>
			<u-input class="input-two" v-model="VehicleCar.vehicleColor"></u-input>
		</view>
		<u-line class="line" margin="0rpx 0rpx 38rpx 0rpx"></u-line>
		<view class="u-line-1 text-five">车辆图片</view>
		<u--image :showLoading="true" :src="VehicleCar.file" width="80px" height="80px"></u--image>
		<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="10">
		</u-upload>
		<view class="u-line-1 text-six">住户信息</view>
		<u-line class="line" margin="0rpx 0rpx 22rpx 0rpx"></u-line>
		<view class="flex-row wrapper-five">
			<u-input class="input" v-model="user.userName"></u-input>
			<view class="u-line-1 text-seven">住户姓名</view>
		</view>
		<u-line class="line" margin="0rpx 0rpx 30rpx 0rpx"></u-line>
		<view class="flex-row wrapper-six">
			<u-input class="input" v-model="user.phone"></u-input>
			<view class="u-line-1 text-eight">手机号码</view>
		</view>
		<u-line class="line" margin="0rpx 0rpx 124rpx 0rpx"></u-line>
		<u-button class="button" type="success" size="medium" @click="AddCar()">
			保存
		</u-button>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				fileList1: [],
				user: {},
				form: {
					userId: '',
					vehicleNumber: '',
					vehicleBrand: '',
					vehicleModel: '',
					vehicleColor: '',
					file: ''
				},
				VehicleCar: {}
			}
		},
		onLoad(data) {
			this.user = uni.getStorageSync('user')
			console.log(this.user)
			const editItem = JSON.parse(decodeURIComponent(data.data));
			this.VehicleCar = editItem
			console.log(this.VehicleCar)
		},
		methods: {
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {

				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://127.0.0.1:9000/serverwg/uploadcl', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							if (res.data != null) {
								this.VehicleCar.file = res.data
							} else {
								alert("网络原因，上传失败")
							}
						}
					});
				})
			},
			AddCar() {
				console.log(this.form)
				this.$axios({
					url: 'serverwg/WjxUpdateVehicleCar',
					data: this.VehicleCar,
					method: 'POST',
					success: (res) => {
						if (res.data.code == 2000) {
							uni.navigateTo({
								url: './Myvehicle',
								success: function(res) {}
							})
						} else if (res.data.code == 5000) {
							alert("添加失败")
						}



					}
				})
			}
		}
	}
</script>
<style lang="scss" scoped>
	/** 全局样式-开始,建议放在公共css样式文件中,比如common.css 或app.vue文件 */

	html,
	body {
		margin: 0;
		width: 100%;
		height: 100%;
		font-size: 16px
	}

	view,
	text,
	image {
		position: relative;
		box-sizing: border-box;
		flex-shrink: 0;
	}

	.flex-col {
		display: flex;
		align-items: flex-start;
		flex-direction: column;
	}

	.flex-row {
		display: flex;
		align-items: flex-start;
	}

	.flex-col .flex-row {
		width: 100%
	}

	/** 全局样式-结束*/

	.homepage {
		padding: 42rpx 0rpx 20rpx 0rpx;

		.wrapper {
			width: 620rpx;
			margin: 0rpx 0rpx 24rpx 130rpx;

			.input {
				width: 100%;
			}

			.text {
				width: 122rpx;
				top: 16rpx;
				left: -94rpx;
				position: absolute;
			}
		}

		.line {
			width: 100%;
		}

		.wrapper-two {
			width: 620rpx;
			margin: 0rpx 0rpx 30rpx 130rpx;

			.input {
				width: 100%;

			}

			.text-two {
				width: 104rpx;
				top: 16rpx;
				left: -102rpx;
				position: absolute;
			}
		}

		.wrapper-three {
			width: 620rpx;
			margin: 0rpx 0rpx 32rpx 130rpx;

			.input {
				width: 100%;

			}

			.text-three {
				width: 104rpx;
				top: 16rpx;
				left: -102rpx;
				position: absolute;
			}
		}

		.wrapper-four {
			width: 722rpx;
			justify-content: space-between;
			margin: 0rpx 0rpx 36rpx 28rpx;

			.text-four {
				width: 104rpx;
				margin-top: 16rpx;
			}

			.input-two {
				width: 620rpx;

			}
		}

		.text-five {
			width: 184rpx;
			margin: 0rpx 0rpx 12rpx 28rpx;
		}

		.upload {
			width: 100%;
			margin-bottom: 70rpx;
		}

		.text-six {
			width: 154rpx;
			margin: 0rpx 0rpx 22rpx 28rpx;
		}

		.wrapper-five {
			width: 594rpx;
			margin: 0rpx 0rpx 30rpx 156rpx;

			.input {
				width: 100%;

			}

			.text-seven {
				width: 154rpx;
				top: 16rpx;
				left: -128rpx;
				position: absolute;
			}
		}

		.wrapper-six {
			width: 594rpx;
			margin-left: 156rpx;

			.input {
				width: 100%;

			}

			.text-eight {
				width: 154rpx;
				top: 16rpx;
				left: -128rpx;
				position: absolute;
			}
		}

		.button {
			width: 644rpx;
			display: block;
			margin-right: auto;
			margin-left: auto;
			background: #2b85e4;
		}
	}
</style>
